import { keyData, nameData, dataTypeData, siteData } from './mysql-data'
import { changeType, backType } from './mod-type'
const { $fn } = window
const listHeight = 600
const Index = ({ database, table_name }) => {
	const config = {
		modal: {
			title 		: '字段',
			width 		: 1000,
		},
		form: {
			labelWidth 	: 's8',
			param: { database, table_name, Null:1, auto_increment:0, site:'last' },
			setParam: ({ Extra }) =>{
				let d = {}
				if(Extra?.includes('DEFAULT_GENERATED')) {
					d =  { Extra: Extra.replace('DEFAULT_GENERATED', '') }
				}
				return d
			},
			onValid: ({ auto_increment, Field }, { tableRef, add }) => {
				if( auto_increment ) {
					let d = tableRef.data().some( v => v.Field !== Field && v.Extra.includes('auto_increment') )
					if( d ){
						$fn.msg.warning('一个表中只能有一个自增: auto_increment')
						return false
					}
				}
				if( add ){
					if( Field ) {
						let d = tableRef.data().some( v => v.Field === Field )
						if( d ){
							$fn.msg.warning(`字段【${Field}】已存在`)
							return false
						}
					}
				}
				return true
			},
			subtxt:1,
			data: [
				{ label: '字段名',			name: 'Field',  			r:1,  half:1, },
				{ label: '常用字段',			name: 'name',				type:'select', half:1, data:nameData, group:1, listHeight, change:({ value, ref, row })=>{
					const { sql, label } = row
					const { Comment, x, y, Type } = sql
					let v =  { ...sql, Field: value }
					v.Comment = Comment ? Comment : label
					//
					const _x = $fn.getRow( config, 'x')
					_x.hide = !x
					$fn.getRow( config, 'y').hide = !y
					//取长度或位数
					_x.half = ['decimal_len', 'numeric_len'].includes(Type) 
					
					ref.setValue(v)
					ref.update()
				}},
				{ label: '数据类型',			name: 'Type', type:'select', data: dataTypeData, r:1, group:1, listHeight, change: v => changeType({ ...v, config })},
				{ label: '长度',			name: 'x', type:'number', hide:1, r:1 },
				{ label: '小数位数',			name: 'y', type:'number', hide:1, half:1, r:1 },
				
				{ label: '是否为空', 		name: 'Null', 				type:'radio', mode:'button', r:1, half:1 },
				{ label: '自增', 			name: 'auto_increment', 	type:'radio', mode:'button', r:1, half:1, width:'auto', unit:'必须是索引'},
				// { label: '虚拟字段', 		name: 'virtual', 			type:'radio', mode:'button', r:1, half:1 },
				{ label: '约束/索引',		name: 'Key',				type:'select', data:keyData },
				{ label: '默认值',			name: 'Default' },
				{ label: '额外参数',			name: 'Extra' },
				{ label: '注释',			name: 'Comment' },
				{ label: '添加位置',			name: 'site',				type:'select', data:siteData, r:1, half:1, change: ({ value, ref })=>{
					$fn.getRow( config, 'site_name').hide = value !== 'after'
					ref.update()
				} },
				{ label: '选择位置',			name: 'site_name',			type:'select', data:[], listHeight, r:1, half:1, hide:1, change:({ value, ref }) =>{
					let Field = ref.getValue('Field')
					if(Field === value){
						$fn.msg.warning(`【${value}】的位置不能是本身`)
						setTimeout(()=>ref.setValue({ site_name:'' }))
					}
				} },
			],
			onMsg : ({ data }) => {
				$fn.msg.warning(data.msg)
			}
		},
		onEnd: ({ tableRef, update, data, mod, formRef }) => {
			$fn.getRow(config, 'site_name').data = tableRef.data().map( v => v.Field )
			$fn.getRow(config, 'Key').disabled = mod && data.Key
			$fn.disabledRows(config, ['Field', 'name'], mod)
			if( mod ){
				const type = backType({ Type:data.Type, config })
				formRef.setValue({ auto_increment: data.Extra.includes('auto_increment'), ...type})
			}
			update()
		}
	}
	return config
}
export default Index